﻿using System;
using System.Drawing;
using System.Windows.Forms;
using PluginInterface;
using SdmxMl.Common;
using SdmxMl.Manager;
using SdmxMl.Structure;
using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace PlugInSelector
{
    /// <summary> Plug-in selector user control</summary>
    public partial class UcPlugSelect : UserControl, IPlugin
    {
        #region Constructor

        public UcPlugSelect()
        {
            InitializeComponent();

            // Get full plug-in list
            if (ReferenceManager.PlugInMgr == null || 
                ReferenceManager.PlugInMgr.CltPlugFullList == null)
            {
                radCarousel1.Visible = false;
                radButtonUpdateList.Enabled = false;
            }
            else
            {
                RadImageItem radImgItem = null;
                this.radCarousel1.Items.Clear();
                foreach (CodeType c in ReferenceManager.PlugInMgr.CltPlugFullList.CodeList)
                {
                    if (c.Id == PluginManager.PLUG_IN_INSTALLER_ID)
                        continue;
                    // Create
                    radImgItem = new RadImageItem();
                    radImgItem.Image = GetPlugImage(c);
                    radImgItem.Name = c.Id;
                    radImgItem.Text = string.Empty;
                    radImgItem.ToolTipText = c.Name.ToString();
                    radImgItem.Visibility = Telerik.WinControls.ElementVisibility.Visible;
                    radImgItem.MouseDown += new MouseEventHandler(carouselItem_MouseDown);
                    this.radCarousel1.Items.Add(radImgItem);             
                }
            }
        }

        /// <summary>
        /// Get Image to associate to plug-in. Image is embedded in annotation of type IMAGE
        /// </summary>
        /// <param name="c">Code holding Plug-in eta information</param>
        /// <returns>The associated image </returns>
        private Image GetPlugImage(CodeType c)
        {
            string serializedImage = string.Empty;
            Image plugImage = null; // get a ressource default image 
            Annotation a = c.AnnotationList.GetAnnotationType("IMAGE");
            if (a != null)
            {
                plugImage = SdmxMl.Helper.ImageHelper.GetPngImageFromString(a.Text.GetForLanguage("en"));
            }
            return plugImage;
        }
        #endregion

        #region Double click Event

        /// <summary>
        /// Since the animation starts on the first click, the double click event is never fired. 
        /// Therefore, we use the MouseDown event and check the number of clicks.
        /// </summary>
        /// <param name="sender"></param><param name="e"></param>
        void carouselItem_MouseDown(object sender, MouseEventArgs e)
        {
            // Double click occured ?
            if (e.Clicks > 1)
            {
                try
                {
                    if (radCarousel1.SelectedItem != null)
                    {
                        // Get current plug-in image selected
                        RadImageItem item = radCarousel1.SelectedItem as RadImageItem;
                        if (item != null)
                        {
                            // Remove or add according to Tab presence
                            if (ReferenceManager.PlugInMgr.PlugInControlLoaded(item.Name))
                                ReferenceManager.PlugInMgr.RemovePlugIn(item.Name);
                            else
                            {
                                // Add plug_in
                                ISdmxHierarchyListItem it =
                                    ReferenceManager.PlugInMgr.CltPlugFullList.CodeList.LocateItem(item.Name);
                                if (it != null)
                                {
                                    ReferenceManager.PlugInMgr.ProcessPlugIn(it as CodeType, true);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    RadMessageBox.Show(this, "Unable to operate on plug-in! \nReason: "
                        + ex.Message, "Caution", MessageBoxButtons.OK, RadMessageIcon.Error);
                }
            }
        }
        #endregion

        #region IPlugin Members

        /// <summary>Event is part of interface but not used </summary>
        public event PluginInterface.PluginEvent PluginEventExposed
        {
            add { }
            remove { }
        }

        public void Init(System.Xml.XmlDocument xDoc, object obExtend)
        {
        }

        public UserControl VisualComponent()
        {
            return this;
        }

        public bool RequiresInit
        {
            get { return false; }
        }

        #endregion

        #region Update list Event

        /// <summary>Update stratup plugin list according to current selections</summary>
        /// <param name="sender"></param> <param name="e"></param>
        private void radButtonUpdateList_Click(object sender, EventArgs e)
        {
            try
            {
            ReferenceManager.PlugInMgr.PersistSelectionAsDefaultMetaList();
            }
            catch (Exception ex)
            {
                RadMessageBox.Show(this, "Unable to update plug-in start list! \nReason: "
                    + ex.Message, "Caution", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }
        #endregion
    }
}
